/* eslint-disable no-new */
import axios from 'axios'
import { Loading } from 'element-ui'

const instance = axios.create({
  baseURL: process.env.baseURL
})

/**
 * 请求拦截器
 */
instance.interceptors.request.use((config) => {
  // todo：处理请求逻辑
  Loading.service({ fullscreen: true })
  return config
})

/**
 * 响应拦截器
 */
instance.interceptors.response.use(
  (response) => {
    // todo：处理响应逻辑
    if (process.browser) {
      Loading.service().close()
    }
    if (response.status.toString().startsWith(2)) {
      return response.data
    }

    return response
  },
  (error) => {
    if (process.browser) {
      Loading.service().close()
    }
    return error.response.data
  }
)

export default instance
